﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI; 
using Runda.EShop.Common.Utility;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;

namespace Runda.EShop.SaaS
{
    public class BasePage:Page
    {
        #region 属性

        public new CrmUserPrincipal User
        {
            get { return base.User as CrmUserPrincipal; }
        }

        /// <summary>
        /// 是否判断权限信息
        /// 对于登录用户均可访问的页面可设置该值为false，以避免不需要的权限配置
        /// </summary>
        public virtual bool IsCheckAccessible
        {
            get
            {
                return true;
            }
        }

        public string CurrentAppPath
        {
            get
            {
                string appPath = Request.RawUrl.ToLower();

                if (Request.ApplicationPath.ToLower() != "/")
                {
                    appPath = appPath.Replace(Request.ApplicationPath.ToLower(), "");
                }

                if (appPath.Contains("?"))
                {
                    appPath = appPath.Substring(0, appPath.IndexOf("?"));
                }
                //return appPath.TrimStart('/');
                return "~/" + appPath.TrimStart('/');
            }
        }

        private bool HasPermission
        {
            get
            {
                if (!string.IsNullOrEmpty(FunctionID))
                {
                    return this.CheckUserHasFuction(1, FunctionID);
                }
                else if (!string.IsNullOrEmpty(CurrentAppPath))
                {
                    return this.CheckUserHasFuction(0, CurrentAppPath);
                }
                return true;
            }
        }

        private string functionid = null;
        public virtual string FunctionID
        {
            get { return functionid; }
        }

        #endregion

        #region Override Methods

        protected override void OnLoad(EventArgs e)
        {
            //Form验证可不用这代码
            if (this.User == null || User.Identity.LoginUser == null)
            {
                string url = Request.ApplicationPath + "/Mock.aspx";
                url = url.Replace("//", "/");
                Response.Redirect(url);
            }
            else
            {
                if (IsCheckAccessible)
                {
                    if (!HasPermission)
                    {
                        throw new HttpException((int)System.Net.HttpStatusCode.Forbidden, "没有权限");
                    }
                }
            }
            base.OnLoad(e);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnError(EventArgs e)
        {
            base.OnError(e);

            string errUrl = Request.Url.Authority + Request.ApplicationPath + "/Mock.aspx?Error=0";
            Exception ex = Server.GetLastError() as Exception;
            if (ex is HttpException)
            {
                //webconfig配置
            }
            else
            {
                string ErrorMsg = ex.Message;
                string StackTrace = ex.StackTrace;
                LogHelper.Write(StackTrace, ErrorMsg, LogCategorySource.FileLog, 0, 0, System.Diagnostics.TraceEventType.Error);
                //errUrl = errUrl.Replace("//", "/");
                //errUrl = "http://" + errUrl;
                //Response.Redirect(errUrl);
            }
        }

        #endregion

        #region public methods
        /// <summary>
        /// 检查权限
        /// </summary>
        /// <param name="CheckType">类型：0 by url,1 by function code</param>
        /// <param name="CheckObject"></param>
        /// <returns></returns>
        public bool CheckUserHasFuction(int CheckType, string CheckObject)
        {
            SystemBll bll = new SystemBll();
            return bll.CheckUserHasFuction(User.Identity.LoginUser.UserID, CheckType, CheckObject);
        }

        protected void DoOperationLog(EnumHelper.OperationType operType,string Content)
        {
            SysLogOperationBll bll = new SysLogOperationBll();
            SysLogOperationEntity logEntity = new SysLogOperationEntity();
            logEntity.OperationType = (int)operType;
            logEntity.UserType = (int)EnumHelper.UserType.Administrator;
            logEntity.OperationUserID = User.Identity.LoginUser.UserID;
            logEntity.OperationUserName = User.Identity.LoginUser.UserName;
            logEntity.CreatedAt = DateTime.Now;
            logEntity.Deleted = false;
            logEntity.Content = Content;
            bll.Insert(logEntity);
        }
        #endregion

        #region Private Methods

        #endregion
    }
}